# -*- coding:utf-8 -*-
from collections import OrderedDict
from apps.goods.models import GoodsCategory, SKUSpecification
from apps.goods.models import GoodsChannel

"""
分类数据
"""


def get_categories():
    """

    :return:
        # 定义一个有序字典对象
    categories = OrderedDict()

    # 对 GoodsChannel 进行 group_id 和 sequence 排序, 获取排序后的结果:
    channels = GoodsChannel.objects.order_by('group_id',
                                             'sequence')

    # 遍历排序后的结果: 得到所有的一级菜单( 即,频道 )
    for channel in channels:
        # 从频道中得到当前的 组id
        group_id = channel.group_id

        # 判断: 如果当前 组id 不在我们的有序字典中:
        if group_id not in categories:
            # 我们就把 组id 添加到 有序字典中
            # 并且作为 key值, value值 是 {'channels': [], 'sub_cats': []}
            categories[group_id] = {
                'channels': [],
                'sub_cats': []
            }

        # 获取当前频道的分类名称
        cat1 = channel.category

        # 给刚刚创建的字典中, 追加具体信息:
        # 即, 给'channels' 后面的 [] 里面添加如下的信息:
        categories[group_id]['channels'].append({
            'id': cat1.id,
            'name': cat1.name,
            'url': channel.url
        })

        # 根据 cat1 的外键反向, 获取下一级(二级菜单)的所有分类数据, 并遍历:
        cat2s = GoodsCategory.objects.filter(parent=cat1)
        # cat1.goodscategory_set.all()
        for cat2 in cat2s:
            # 创建一个新的列表:
            cat2.sub_cats = []
            cat3s = GoodsCategory.objects.filter(parent=cat2)
            # 根据 cat2 的外键反向, 获取下一级(三级菜单)的所有分类数据, 并遍历:
            for cat3 in cat3s:
                # 拼接新的列表: key: 二级菜单名称, value: 三级菜单组成的列表
                cat2.sub_cats.append(cat3)
            # 所有内容在增加到 一级菜单生成的 有序字典中去:
            categories[group_id]['sub_cats'].append(cat2)

    return categories

    """
    # 定义一个有序字典对象
    categories = OrderedDict()
    # 对 GoodsChannel 进行 group_id 和 sequence 排序, 获取排序后的结果:
    channels = GoodsChannel.objects.order_by('group_id',
                                             'sequence')
    # 遍历排序后的结果: 得到所有的一级菜单( 即,频道 )
    for channel in channels:
        # 从频道中得到当前的 组id
        group_id = channel.group_id
        # 判断: 如果当前 组id 不在我们的有序字典中:
        if group_id not in categories:
            # 我们就把 组id 添加到 有序字典中
            # 并且作为 key值, value值 是 {'channels': [], 'sub_cats': []}
            categories[group_id] = {
                'channels': [],
                'sub_cats': []
            }
        # 获取当前频道的分类名称
        cat1 = channel.category
        # 给刚刚创建的字典中, 追加具体信息:
        # 即, 给'channels' 后面的 [] 里面添加如下的信息:
        categories[group_id]['channels'].append({
            'id': cat1.id,
            'name': cat1.name,
            'url': channel.url
        })

        # 根据 cat1 的外键反向, 获取下一级(二级菜单)的所有分类数据, 并遍历:
        cat2s = GoodsCategory.objects.filter(parent=cat1)
        for cat2 in cat2s:

            # categories[group_id]['sub_cats']['name'] = cat2.name
            cat2.sub_cats = []
            # print(cat2.sub_cats)
            # # 创建一个新的列表:
            cat3s = GoodsCategory.objects.filter(parent=cat2)
            # # 根据 cat2 的外键反向, 获取下一级(三级菜单)的所有分类数据, 并遍历:
            for cat3 in cat3s:
                #     # 拼接新的列表: key: 二级菜单名称, value: 三级菜单组成的列表
                #     # print(cat3)
                item = {}
                item['id'] = cat3.id
                item['name'] = cat3.name
                cat2.sub_cats.append(item)
            # # 所有内容在增加到 一级菜单生成的 有序字典中去:
            # categories[group_id]['sub_cats'].append(cat2)
            categories[group_id]['sub_cats'].append(
                {
                    'id': cat2.id,
                    'name': cat2.name,
                    'sub_cats':cat2.sub_cats
                }
            )
    # print(categories)
    return categories


"""
面包屑
"""


def get_breadcrumb(category):
    '''接收最低级别的类别, 获取各个类别的名称, 返回'''

    dict = {
        'cat1': '',
        'cat2': '',
        'cat3': '',
    }

    if category.parent is None:
        dict['cat1'] = category.name
    elif category.parent.parent is None:
        dict['cat2'] = category.name
        dict['cat1'] = category.parent.name
    else:
        dict['cat3'] = category.name
        dict['cat2'] = category.parent.name
        dict['cat1'] = category.parent.parent.name

    return dict


# 3. 获取规格数据的函数
# def get_specs(sku):
#     # 1. 构建字典: 通过选项反查sku_id的字典
#     sku_specs = SKUSpecification.objects.filter(
#         spec__spu=sku.spu).order_by('option')
#     basic_dict = {}
#     for sku_spec in sku_specs:
#         sku_idl = str(sku_spec.sku.id)
#         option_id = str(sku_spec.option.id)
#         option_id_list = basic_dict.get(sku_idl)
#         if not option_id_list:
#             option_id_list = []
#             basic_dict[sku_idl] = option_id_list
#         option_id_list.append(option_id)
#
#     find_sku_dict = {}
#     for (key, value) in basic_dict.items():
#         new_key = '_'.join(value)
#         find_sku_dict[new_key] = key
#     # print(find_sku_dict)
#
#     # 2. 遍历处理数据
#     specs = sku.spu.specs.all()
#     for spec in specs:
#         spec.spec_options = spec.options.all()
#         for option in spec.spec_options:
#             # option.sku_id = ?
#             # 0. 已经拥有选项id:1
#             option_id = option.id  # 1
#
#             # 1. 找到当前选项1关联到的规格所有选项的id [1, 2]
#             spec_options = option.spec.options.all()
#             spec_option_ids = [str(spec_option.id) for spec_option in spec_options]
#
#             # 2. 找到当前sku_id=3的sku关联到选项的id [2, 3]
#             # 3. 找出在2中的数据，但是不在1中的数据   [3]
#             sku_specs = sku.specs.all()
#             # sku_option_ids = [str(sku_spec.option.id) for sku_spec in sku_specs]
#             need_find_option_ids = []
#             for sku_spec in sku_specs:
#                 sku_spec_option_id = str(sku_spec.option.id)
#                 if sku_spec_option_id not in spec_option_ids:
#                     need_find_option_ids.append(str(sku_spec.option.id))
#
#             # 3. 把已经拥有选项id:1  丢到3这个数组中 并进行增排序  [1, 3]
#             need_find_option_ids.append(str(option_id))
#             need_find_option_ids.sort()
#             # print(need_find_option_ids)
#
#             # 4. 利用[1, 3]拼出寻找sku_id的key  '1_3', 之后利用key去find_sku_dict去找出要找的sku_id
#             find_sku_key = '_'.join(need_find_option_ids)
#             sku_id = find_sku_dict.get(find_sku_key)
#             if sku_id:
#                 option.sku_id = int(sku_id)
#
#     return specs